<?php
class User extends DBSQL
{
	public function __construct()
	{
		parent::__construct();
	}
	/**
	 * 功能：检测用户名是否存在
	 * 参数：$name 用户名
	 * 返回：TRUE OR FALSE
	 */
	public function CheckUserNameExist($name)
	{
		$sql = "SELECT F_ID FROM em_user_info WHERE F_USER_NAME='$name'";
		$r = $this->select($sql);
		if($r[0][0]) return true;
		else return false;
	}
	/**
	 * 功能：用户注册
	 * 参数：$array 注册提交信息
	 */
	public function Register($array)
	{
		$this->begintransaction();
		try{
			$data = array();
			$data['F_USER_NAME'] = $array['username'];
			$data['F_USER_PASSWORD'] = md5($array['password']);
			$data['F_USER_NICKNAME'] = $array['NickName'];
			$data['F_USER_EMAIL'] = $array['Email'];
			$data['F_USER_REGTIME'] = time();
			$userid = $this->insertData("em_user_info",$data);		//插入数据到表em_user_info
			$data = array();
			$data['F_BLOG_NAME']=$array['Blog'];
			$data['F_BLOG_DEFAULT_SKINS']='default';
			$data['F_BLOG_REGTIME']=time();
			$blogid = $this->insertData("em_blog_info",$data);
			$data = array();
			$data['F_ID_BLOG_INFO']=$blogid;
			$data['F_ID_USER_INFO']=$userid;
			$data['F_BLOG_IS_LOCKED']=0;
			$data['F_BLOG_PERM_COMMENTS']=1;
			$this->insertData("ee_blog_user",$data);
			$data = array();
			$data['F_ID_BLOG_INFO'] = $blogid;
			$data['F_CATEGORIES_NAME'] = '默认分类';
			$data['F_CATEGORIES_DESCRIPTION'] = '默认分类';
			$data['F_CATEGORIES_DEFAULT'] = 1;
			$this->insertData("ee_blog_categories2",$data);
		}catch(exception $e){
			$this->rollback();
			return false;
		}
		$this->commit();
		return true;
	}
	/**
	* 功能：用户登录
	* 参数：$name 用户名,$password 用户密码
	* 返回：true or false
	*/
	public function Login($name,$password)
	{
		$name = strtoupper($name);
		$password = md5($password);
		$sql = "SELECT F_ID,F_USER_NAME,F_USER_NICKNAME FROM em_user_info ";
		$sql .= "WHERE UPPER(F_USER_NAME)='$name' AND F_USER_PASSWORD='$password'";
		$r = $this->select($sql);
		if($r[0][F_ID])
		{
			$_SESSION['User_F_ID']=$r[0][F_ID];
			if(!isset($r[0]['F_USER_NICKNAME']))
				$_SESSION['User_Name']=$r[0]['F_USER_NAME'];
			else
				$_SESSION['User_Nickname']=$r[0]['F_USER_NICKNAME'];
				$_SESSION['User_Name']=$r[0]['F_USER_NAME'];
			return true;
		}else{
			return false;
		}
	}
	/**
	* 功能：显示用户信息
	* 参数：$userid 用户ID
	* 返回：数组
	*/
	public function GetUserList($userid)
	{
		$sql = "SELECT * FROM em_user_info WHERE F_ID=$userid ";
		$sql .= "ORDER BY F_USER_NAME DESC";
		$r = $this->select($sql);
		return $r[0];
	}
	/**
	* 功能：取出对应blog的id
	* 参数：$userid 用户ID
	* 返回：blog的id
	*/
	public function GetBlogId($userid) {
		$sql = "SELECT F_ID_BLOG_INFO FROM ee_blog_user WHERE F_ID_USER_INFO = $userid";
		$r = $this->select($sql);
		return $r[0][0];
	}
}
?>